﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBService;

public partial class PhotoUploaderPopUp : System.Web.UI.Page
{
    int CompanyId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!int.TryParse((Request.QueryString["id"] ?? ""), out CompanyId))
        {
            Response.Redirect("default.aspx");
        }
    }

    protected void OK_Click(object sender, EventArgs e)
    {
        try
        {
            ModelAdapter service = new ModelAdapter();
            List<string> images = new List<string>();
            var hfc = Request.Files;
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    if (new String[] { ".png", ".jpg", ".gif", ".jpeg" }.Contains(System.IO.Path.GetExtension(hpf.FileName)))
                    {
                        string img = Guid.NewGuid().ToString().Substring(0, 10) +
                          System.IO.Path.GetFileName(hpf.FileName);
                        images.Add(img);
                        hpf.SaveAs(Server.MapPath("upload") + "\\" + img);
                    }
                    else
                    {
                        throw new Exception("ფაილის დაშვებული ფორმატებია:  .png .jpg .gif .jpeg");
                    }
                }
            }
            service.UpdateCompanyGallery(CompanyId, images);
        }
        catch (Exception ex)
        {
            Script.Text = "<script>alert('" + ex.Message + "'</script>";
        }
    }
}